<template>
  <view class="safe-bottom">
    <view class="">
      <goods-order-address :address="address"></goods-order-address>
    </view>
    <!-- 商品详情 -->
    <template v-if="shopList.length">
      <view class="" style="padding-bottom: 100rpx">
        <view class=" " v-for="(item, index) in shopList" :key="index">
          <goods-order-item
            :index="index"
            :item="item"
            :is_special="is_special"
            @coupupSure="coupupSure"
          ></goods-order-item>
        </view>
		<view class="u-m-t-10 u-p-x-30 bg-white" v-if="is_special != 1">
		  <u-cell-item
		    :title="`该订单需要${need_credit_discount}记账点才能支付`"
		    :arrow="false"
		    hoverClass="none"
		  >
		  </u-cell-item>
		</view>
		<view class=" u-p-x-30 bg-white" v-if="is_special != 1">
		  <u-cell-item
		    :title="`您账户目前拥有${userInfo.credit4}记账点`"
		    :arrow="false"
		    hoverClass="none"
		  >
		  </u-cell-item>
		</view>
        <!-- <view class=" u-p-x-30 bg-white" v-if="is_special != 1">
          <u-cell-item
            :title="`使用${detail.credit2_number}记账点抵扣￥${detail.credit_money}`"
            :arrow="false"
            hoverClass="none"
          >
		  	<u-checkbox
			v-if="!!goods1_credit4_dikou && order_type == 1 && !(goods1_credit4_dikou > 0) || !!goods4_credit4_dikou && order_type == 4 && !(goods4_credit4_dikou > 0)"
		  	  v-model="is_creditdiscount"
		  	  style="margin-right: 0"
		  	  shape="circle"
		  	  @change="checkboxGroupClick"
		  	></u-checkbox>
          </u-cell-item>
        </view> -->
		
      </view>
    </template>

    <view class="position-fixed left-0 right-0 bottom-0" style="z-index: 2">
      <view class="bg-white safe-bottom">
        <view
          class="flex align-center justify-between u-p-x-30 u-border-top"
          style="height: 100rpx"
        >
          <view class="flex align-center">
            <text class="u-font-30 u-m-r-20">实付金额</text>
            <text class="u-font-30 text-danger"
              >¥{{ order_pay_price || "0.00" }}</text
            >
          </view>
          <view class="">
            <dy-button
              width="200rpx"
              backgroundColor="var(--mainColor)"
              size="medium"
              @click="sureOrder"
              >立即支付</dy-button
            >
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { GoodsCenterApi,MineCenterApi } from "@/api/serves.js";
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      type: "commonm", // common商品下单  cart 购物车下单
      detail: {},
      shopList: [], // 店铺列表
      order_pay_price: "", // 实付金额
      address: null, // 收货地址
      is_special: 0,
      formData: [], // [{merch_id: '', goods_id: '', goods_sku_id: ''}]
      is_creditdiscount: true, // 是否使用积分抵扣
      notes: [], // 备注
      couponObj: {}, // 优惠券Id和店铺id 键值对
      orderInfoList: [], // 优惠劵 优惠劵id 以及备注
      goodsType: 1,
	  order_type:'', // 1 普通商品  4 专区商品
	  goods1_credit4_dikou:'', //普通商品抵扣比例
	  goods4_credit4_dikou:'', //专区商品抵扣比例
	  need_credit_discount:'', // 下单需要记账点
    };
  },
  onLoad(options) {
	this.$store.dispatch('user/getUserInfo')
    uni.$on("addressClick", () => {
      this._orderAffirm();
    });
    console.log("options", options);
    let orderInfo = JSON.parse(decodeURIComponent(options.orderInfo));
    // console.log('orderInfo', orderInfo);
    this.type = options.type || "common";
    this.goodsType = options.goodsType || 1;
    this.formData = orderInfo;
    this._orderAffirm();
  },
  onUnload() {
    uni.$off("addressClick");
  },
  computed:{
	...mapGetters('user',['userInfo'])  
  },
  methods: {
	  // 获取配置
	  getConfig() {
	  	MineCenterApi.getConfig().then(res => {
	  		console.log('获取配置', res);
	  		if (res.code == 1) {
				this.goods1_credit4_dikou = res.data.data.goods1_credit4_dikou
	  			this.goods4_credit4_dikou = res.data.data.goods4_credit4_dikou
	  			console.log('专区商品抵扣比例',this.goods4_credit4_dikou);
	  			console.log('普通商品抵扣比例',this.goods1_credit4_dikou);
	  		} else {
	  			console.log(res);
	  		}
	  	})
	  },
    // 商品下单详情
    _orderAffirm() {
      let params = this.formartParams();
      console.log(999, params);
      uni.showLoading();
	   this.getConfig()
      if (this.type == "common") return this._orderCcmmonInfo(params);
      if (this.type == "cart") return this._orderCartInfo(params);
    },
    _orderCcmmonInfo(params) {
      GoodsCenterApi.orderBuyNow(params).then((res) => {
        uni.hideLoading();
        console.log("购买详情-商品详情页", res);
        if (res.code == 1) {
          this.formartRes(res);
        } else {
          this.$u.toast(res.msg);
        }
      });
    },
    _orderCartInfo(params) {
      GoodsCenterApi.getlists({ ...params, goods_type: this.goodsType }).then((res) => {
        uni.hideLoading();
        console.log("购买详情-购物车", res);
        if (res.code == 1) {
          this.formartRes(res);
        } else {
          this.$u.toast(res.msg);
        }
      });
    },
    formartRes(res) {
      console.log('44343',res);
      this.detail = res.data;
      this.address = res.data.address;
	  this.order_type = res.data.order_type
	  this.need_credit_discount = res.data.need_credit_discount
      if (this.address) {
        this.address.Area = Object.values(this.address.Area);
      }
      if (Array.isArray(res.data.merch_goods_list)) {
        this.shopList = res.data.merch_goods_list;
        if (!this.orderInfoList.length)
          this.orderInfoList = new Array(this.shopList.length).fill("");
      } else {
        this.shopList = Object.values(res.data.merch_goods_list);
        if (!this.orderInfoList.length)
          this.orderInfoList = new Array(this.shopList.length).fill("");
      }
      // console.log(8998, this.shopList);
      this.order_pay_price = res.data.order_pay_price;
    },
    // 下单去支付
    sureOrder() {
      if (!this.address) return this.$Toast("请选择收货地址");
      let payParams = this.formartParams();
      payParams.note = this.orderInfoList
        .map((v) => {
          return v.note || "";
        })
        .join(",");
      let fun = this.type == "common" ? "buyNow_pay" : "cart_pay";
      this.$ShowLoading();
      GoodsCenterApi[fun]({ ...payParams, goods_type: this.goodsType,distribution_type:'1'}).then((res) => {
        console.log("商品下单成功，待支付", res);
        this.$HideLoading();
        if (res.code == 1) {
          let params = {
            order_no: res.data.order_no,
            from: this.type,
          };
          uni.redirectTo({
            url:
              "/pages/goodsDetail/goodsOrderPay/goodsOrderPay?orderInfo=" +
              JSON.stringify(params),
          });
        } else {
          this.$Toast(res.msg);
        }
      });
    },
    formartParams() {
      let goods_id_list = [];
      let goods_sku_id_list = [];
      this.formData.forEach((item) => {
        goods_id_list.push(item.goods_id);
        goods_sku_id_list.push(item.goods_sku_id);
      });
      let params = {
        goods_id: goods_id_list.join(","),
        goods_sku_id: goods_sku_id_list.join(","),
      };
      if (this.type == "common") {
        params.goods_num = this.formData[0].goods_num;
        params.merch_id =
          (this.orderInfoList.length && this.orderInfoList[0].merch_id) || "";
        params.coupon_id =
          (this.orderInfoList.length && this.orderInfoList[0].id) || "";
      }
      if (this.type == "cart") {
        params.type = 1;
        let merch_ids = [];
        let coupon_ids = [];
        for (let v of this.orderInfoList) {
          if (v && v.id) {
            merch_ids.push(v.merch_id);
            coupon_ids.push(v.id);
          }
        }
        params.merch_id = merch_ids.join(",");
        params.coupon_id = coupon_ids.join(",");
      }
      params.is_creditdiscount = this.is_creditdiscount ? 1 : 0;
      return params;
    },
    // 是否使用积分
    checkboxGroupClick(e) {
      console.log("是否使用积分", e);
      this.is_creditdiscount = e.value ? 1 : 0;
      console.log(11, this.orderInfoList);
      this._orderAffirm();
    },
    // 优惠劵选择
    coupupSure(e) {
      this.orderInfoList.splice(e.index, 1, e);
      // console.log(111, this.orderInfoList);
      this._orderAffirm();
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .u-cell {
  padding: 15rpx 0 !important;
}
::v-deep .u-cell_title {
  font-weight: bold !important;
}
::v-deep .u-field {
  padding-left: 0 !important;
  padding-right: 0 !important;
}
::v-deep .u-checkbox__label {
  margin-right: 0;
}
</style>
